package com.mao.action.voucher;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.mao.action.BaseAction;
import com.mao.action.jsonvo.searchcenter.JsonVoVoucherProduct;
import com.mao.action.jsonvo.voucher.JsonVoVoucher;
import com.mao.model.organization.Organization;
import com.mao.model.product.Product;
import com.mao.model.voucher.ProductWareHouseVoucher;
import com.mao.model.voucher.VoucherBase;
import com.mao.model.voucher.VoucherType;
import com.mao.model.voucher.VoucherVerityType;
import com.mao.model.voucher.WareHouseVoucher;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.common.ICommonService;
import com.mao.service.organization.IOrganizationService;
import com.mao.service.product.IProductService;
import com.mao.service.productstock.IProductStockService;
import com.mao.service.searchvoucher.ISearchVoucherService;
import com.mao.service.voucher.IProductWareHouseVoucherService;
import com.mao.service.voucher.IVoucherCommonService;
import com.mao.service.voucher.IWareHouseVoucherService;
import com.mao.service.warehouse.IWareHouseService;
import com.mao.util.SessionHelper;

public class WareHouseVoucherAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private WareHouseVoucher wv;

	private IWareHouseVoucherService wareHouseVoucherService;

	private IOrganizationService organizationService;

	private IWareHouseService wareHouseService;

	private IProductService productService;

	private IProductWareHouseVoucherService pwvService;

	private IVoucherCommonService voucherCommonService;

	private ISearchVoucherService searchVoucherService;

	private IProductStockService productStockService;

	private Organization organization;

	private WareHouse wareHouse;

	private String products;

	private ProductWareHouseVoucher pwv;

	private ICommonService commonService;

	private String name;

	private JsonVoVoucher returnVoucher;

	private String id;

	private List<JsonVoVoucherProduct> pros;

	private VoucherType voucherType;

	// 通用废除单据方法
	public String delVoucher() {
		VoucherBase vb = searchVoucherService.getVoucherById(
				voucherType.getVoucherClassName(), Long.valueOf(id));
		vb.setStatus(VoucherBase.STATUS_DELETED);
		vb.setIsverity(VoucherVerityType.cancel);
		voucherCommonService.saveOrUpdateVoucher(voucherType, vb);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(vb.getId());
		returnVoucher.setVoucherNo(vb.getVoucherNo());
		return SUCCESS;
	}

	public String execute() {
		if (id != null) {
			wv = (WareHouseVoucher) searchVoucherService.getVoucherById(
					VoucherType.warehouse.getVoucherClassName(),
					Long.valueOf(id));
		}
		return SUCCESS;
	}

	// 查看单据详情中，查看单据所包含的商品
	public String getProductsByVoucher() {
		pros = searchVoucherService.searchProductsByVoucher(Long.valueOf(id),
				VoucherType.warehouse);
		return SUCCESS;
	}

	public String saveWareHouseVoucher() {
		organization = organizationService.getOrganizationById(wv.getOrg()
				.getId());
		wareHouse = wareHouseService
				.getWareHouseById(wv.getWareHouse().getId());
		wv.setStore(SessionHelper.getSessionUser().getStore());
		wv.setOrg(organization);
		wv.setWareHouse(wareHouse);
		wv.setDatetime(new Date());
		wv.setVoucherType(VoucherType.warehouse);
		wv.setIsverity(VoucherVerityType.notverity);
		wv.setUser(SessionHelper.getSessionUser());
		String voucherNo = commonService.getVoucherNo(SessionHelper
				.getSessionUser().getId(), "JHD", WareHouseVoucher.class
				.getName());
		wv.setVoucherNo(voucherNo);
		wareHouseVoucherService.saveWareHouseVoucher(wv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			pwv = new ProductWareHouseVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			pwv.setProduct(singlePro);
			pwv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			pwv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + pwv.getAmount();
			pwv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(pwv.getSubtotal());
			pwv.setVoucher(wv);
			pwv.setStore(SessionHelper.getSessionUser().getStore());
			pwv.setVoucherType(VoucherType.warehouse);
			pwvService.savePwv(pwv);
		}
		// 再存单据的商品总量
		wv.setProAmount(proAmount);
		// 再存单据的总价
		wv.setTotalPrice(totalPrice);
		wareHouseVoucherService.saveWareHouseVoucher(wv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(wv.getId());
		returnVoucher.setVoucherNo(voucherNo);
		return SUCCESS;
	}

	// 审核单据
	public String veriyVoucher() {
		organization = organizationService.getOrganizationById(wv.getOrg()
				.getId());
		wareHouse = wareHouseService
				.getWareHouseById(wv.getWareHouse().getId());
		WareHouseVoucher updateWv = (WareHouseVoucher) searchVoucherService
				.getVoucherById(VoucherType.warehouse.getVoucherClassName(),
						Long.valueOf(id));
		updateWv.setTransactionAmount(wv.getTransactionAmount());
		updateWv.setReceiveAmount(wv.getReceiveAmount());
		updateWv.setNo(wv.getNo());
		updateWv.setNote(wv.getNote());
		updateWv.setOrg(organization);
		updateWv.setWareHouse(wareHouse);
		updateWv.setUpdatetime(new Date());
		updateWv.setIsverity(VoucherVerityType.verityed);
		updateWv.setUser(SessionHelper.getSessionUser());
		wareHouseVoucherService.saveWareHouseVoucher(updateWv);
		String[] prosArray = products.split(",");
		String[] pro = null;
		Product singlePro = null;
		Integer proAmount = 0;
		BigDecimal totalPrice = new BigDecimal(0); 
		for (int i = 0; i < prosArray.length; i++) {
			pwv = new ProductWareHouseVoucher();
			pro = prosArray[i].split("_");
			singlePro = productService.getProductById(Long.valueOf(pro[0]));
			pwv.setProduct(singlePro);
			pwv.setUnitPrice(BigDecimal.valueOf(Double.valueOf(pro[1])));
			pwv.setAmount(Integer.valueOf(pro[2]));
			// 商品总量计算
			proAmount = proAmount + pwv.getAmount();
			pwv.setSubtotal(BigDecimal.valueOf(Double.valueOf(pro[3])));
			// 单据总价计算
			totalPrice = totalPrice.add(pwv.getSubtotal());
			pwv.setVoucher(updateWv);
			pwv.setStore(SessionHelper.getSessionUser().getStore());
			pwv.setVoucherType(VoucherType.warehouse);
			pwvService.savePwv(pwv);
		}
		// 单据总价计算
		updateWv.setProAmount(proAmount);
		// 再存单据的总价
		updateWv.setTotalPrice(totalPrice);
		wareHouseVoucherService.saveWareHouseVoucher(updateWv);
		returnVoucher = new JsonVoVoucher();
		returnVoucher.setId(updateWv.getId());
		returnVoucher.setVoucherNo(updateWv.getVoucherNo());
		return SUCCESS;
	}

	public String toAddWareHouseVoucher() {
		return SUCCESS;
	}

	public WareHouseVoucher getWv() {
		return wv;
	}

	public void setWv(WareHouseVoucher wv) {
		this.wv = wv;
	}

	public IWareHouseVoucherService getWareHouseVoucherService() {
		return wareHouseVoucherService;
	}

	public void setWareHouseVoucherService(
			IWareHouseVoucherService wareHouseVoucherService) {
		this.wareHouseVoucherService = wareHouseVoucherService;
	}

	public IOrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(IOrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public IWareHouseService getWareHouseService() {
		return wareHouseService;
	}

	public void setWareHouseService(IWareHouseService wareHouseService) {
		this.wareHouseService = wareHouseService;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public WareHouse getWareHouse() {
		return wareHouse;
	}

	public void setWareHouse(WareHouse wareHouse) {
		this.wareHouse = wareHouse;
	}

	public String getProducts() {
		return products;
	}

	public void setProducts(String products) {
		this.products = products;
	}

	public IProductService getProductService() {
		return productService;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public ProductWareHouseVoucher getPwv() {
		return pwv;
	}

	public void setPwv(ProductWareHouseVoucher pwv) {
		this.pwv = pwv;
	}

	public IProductWareHouseVoucherService getPwvService() {
		return pwvService;
	}

	public void setPwvService(IProductWareHouseVoucherService pwvService) {
		this.pwvService = pwvService;
	}

	public ICommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public JsonVoVoucher getReturnVoucher() {
		return returnVoucher;
	}

	public void setReturnVoucher(JsonVoVoucher returnVoucher) {
		this.returnVoucher = returnVoucher;
	}

	public ISearchVoucherService getSearchVoucherService() {
		return searchVoucherService;
	}

	public void setSearchVoucherService(
			ISearchVoucherService searchVoucherService) {
		this.searchVoucherService = searchVoucherService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<JsonVoVoucherProduct> getPros() {
		return pros;
	}

	public void setPros(List<JsonVoVoucherProduct> pros) {
		this.pros = pros;
	}

	public IVoucherCommonService getVoucherCommonService() {
		return voucherCommonService;
	}

	public void setVoucherCommonService(
			IVoucherCommonService voucherCommonService) {
		this.voucherCommonService = voucherCommonService;
	}

	public VoucherType getVoucherType() {
		return voucherType;
	}

	public void setVoucherType(VoucherType voucherType) {
		this.voucherType = voucherType;
	}

	public IProductStockService getProductStockService() {
		return productStockService;
	}

	public void setProductStockService(IProductStockService productStockService) {
		this.productStockService = productStockService;
	}

}
